﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;

namespace RaisingForce.Raise.Presentation.Localization
{
    [AttributeUsage(
        AttributeTargets.Assembly,
        Inherited = false,
        AllowMultiple = false)]
    sealed public class AssemblyLocalizationInfoAttribute : Attribute
    {
        #region Constructors

        public AssemblyLocalizationInfoAttribute()
            : base()
        { }

        #endregion

        #region Properties

        public string ResourcesNamespace { get; set; }

        public string AppNamespace { get; set; }

        #endregion

        #region Private Declarations

        private const string resourcesClassPostfix = ".Properties.Resources";

        private const string appClassPostfix = ".App";

        private static string getAssemblyName(Assembly assembly)
        {
            Contract.Requires(assembly != null);
            Contract.Ensures(Contract.Result<string>() != null);
            return assembly.GetName().Name;
        }

        private static AssemblyLocalizationInfoAttribute getAssemblyAttribute(
            Assembly searchingAssembly)
        {
            Contract.Requires(searchingAssembly != null);
            AssemblyLocalizationInfoAttribute attribute =
                searchingAssembly
                .GetCustomAttributes(
                    typeof(AssemblyLocalizationInfoAttribute),
                    inherit: false)
                .OfType<AssemblyLocalizationInfoAttribute>()
                .FirstOrDefault();
            return attribute;
        }

        #endregion

        #region Public Methods

        public string GetResourcesLocation()
        {
            return this.ResourcesNamespace != null ?
                this.ResourcesNamespace + resourcesClassPostfix :
                null;
        }

        public string GetAppLocation()
        {
            return this.AppNamespace != null ?
                this.AppNamespace + appClassPostfix :
                null;
        }

        #endregion

        #region Static Resources Location Methods

        public static string TryFindResourcesLocation(
            Assembly searchingAssembly)
        {
            if (searchingAssembly == null)
            {
                throw new ArgumentNullException("searchingAssembly");
            }
            Contract.EndContractBlock();
            var attribute = getAssemblyAttribute(searchingAssembly);
            return attribute != null ? attribute.GetResourcesLocation() : null;
        }

        public static string GetDefaultResourcesLocation(
            Assembly searchingAssembly)
        {
            if (searchingAssembly == null)
            {
                throw new ArgumentNullException("searchingAssembly");
            }
            Contract.Ensures(Contract.Result<string>() != null);
            Contract.EndContractBlock();
            return getAssemblyName(searchingAssembly) + resourcesClassPostfix;
        }

        public static string FindResourcesLocation(
            Assembly searchingAssembly)
        {
            if (searchingAssembly == null)
            {
                throw new ArgumentNullException("searchingAssembly");
            }
            Contract.Ensures(Contract.Result<string>() != null);
            Contract.EndContractBlock();
            return TryFindResourcesLocation(searchingAssembly)
                ?? GetDefaultResourcesLocation(searchingAssembly);
        }

        #endregion

        #region Static App Location Methods

        public static string TryFindAppLocation(
            Assembly searchingAssembly)
        {
            if (searchingAssembly == null)
            {
                throw new ArgumentNullException("searchingAssembly");
            }
            Contract.EndContractBlock();
            var attribute = getAssemblyAttribute(searchingAssembly);
            return attribute != null ? attribute.GetAppLocation() : null;
        }

        public static string GetDefaultAppLocation(
            Assembly searchingAssembly)
        {
            if (searchingAssembly == null)
            {
                throw new ArgumentNullException("searchingAssembly");
            }
            Contract.Ensures(Contract.Result<string>() != null);
            Contract.EndContractBlock();
            return getAssemblyName(searchingAssembly) + appClassPostfix;
        }

        public static string FindAppLocation(
            Assembly searchingAssembly)
        {
            if (searchingAssembly == null)
            {
                throw new ArgumentNullException("searchingAssembly");
            }
            Contract.Ensures(Contract.Result<string>() != null);
            Contract.EndContractBlock();
            return TryFindAppLocation(searchingAssembly)
                ?? GetDefaultAppLocation(searchingAssembly);
        }

        #endregion
    }
}
